package com.jn.algorithm.day009.question26;

import java.util.*;

/**
 * @author 江南大学1033190417
 * @date 2022/4/24 21:58
 */
public class Solution {

    //模拟
    public int removeDuplicates(int[] nums) {
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i - 1] == nums[i]) {
                continue;
            }
            nums[count++] = nums[i];
        }
        return count;
    }

    //双指针
    /*
       两个指针都指向第1个元素，fast，low
       如果fast指针和上次所指的不一样，则把当前fast指向的值赋值给low指向的，并对low++
       无论如何fast++，直至数组下标越界
     */
    public int removeDuplicates2(int[] nums) {
        int length = nums.length;
        if (length <= 1) {
            return length;
        }
        int fast = 1, low = 1;
        while (fast < length) {
            if (nums[fast]!=nums[fast-1]){
                nums[low]=nums[fast];
                ++low;
            }
            ++fast;
        }
        return low;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{1,2};
        System.out.println(solution.removeDuplicates2(nums));
    }
}
